<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Nilai extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_nilai');
		//$this->load->model('mdl_fakultas');
		//$this->load->model('mdl_prodi');
		//$this->output->enable_profiler(true);
	}
	
	function index(){
		$this->load->view('perkuliahan/nilai');
	}
	
	
	function grid(){
		$data = $this->mdl_nilai->getdata2();
		echo $this->mdl_nilai->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_input(){
		$data = $this->mdl_nilai->getdata_input();
		echo $this->mdl_nilai->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_edit(){
		$data = $this->mdl_nilai->getdata();
		echo $this->mdl_nilai->togrid($data['row_data'], $data['row_count']);
	}
	
	function save(){
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		//var_dump($_POST['data_nilai']);
		
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		/* $cekdate =  date('Y-m-d H:i:s');
		$tgl_nilai = $data['data_nilai']['rows'][0]['TglNilai'];
		if($cekdate <= $tgl_nilai){ */
		$result = $this->mdl_nilai->Update_Nilai($data);
		/* }
		else{
			$data['pesan_error']='Waktu Penilaian Telah Habis'; 
		} */
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function add2(){		
		$this->load->view('perkuliahan/nilai_form2');
	}
	
	function edit(){		
		$this->load->view('perkuliahan/nilai_perbaikan');
	}
	
	//laporan
	function laporan_pdf($kd_prodi,$kd_matakuliah,$kd_dosen,$kd_kelas){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		$fil['kd_kelas'] = $kd_kelas;
		
		 //$data['nama'] = '';
		 $data['namaUniv'] = 'STMIK BANDUNG';
		 $data['alamatUniv'] = 'Jl.Phh.Mustofa No. 39. Grand Surapati Core (SUCORE) Blok M No.19, Telp.022 - 7207777';
		 $data['kotaUniv'] = 'Bandung, Jawa Barat';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_nilai->get_pdf($fil);
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('perkuliahan/nilai_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('exemple00.pdf');
	}
	
	function pdf($kd_prodi,$kd_matakuliah,$kd_dosen,$kd_kelas){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		$fil['kd_kelas'] = $kd_kelas;
		
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_nilai->get_pdf($fil);
		
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}

		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		#LOGO HEADER
		$this->our_pdf->Image('asset/images/logo.png',10,3);
		//$this->our_pdf->Cell(110,9,'UNIVERSITAS OTTOW GEISSLER PAPUA',0,0,'C');
		
		// judul laporan
		//$this->our_pdf->setFont('arial','B',18);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'DAFTAR NILAI MAHASISWA';
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',10);
		/*$posY = 11;
		$posX = 10;
		$midX = 150;
		$title = 'DAFTAR NILAI';*/
		$namas = ($fil['kd_matakuliah']!='')?$this->mdl_nilai->getNameByKodeMK($fil['kd_matakuliah']):'';
		$dosens = ($fil['kd_dosen']!='')?$this->mdl_nilai->getNameByKoDosen($fil['kd_dosen']):'';
		//$prodis = ($fil['kd_prodi']!='')?$this->mdl_khs->getProdiByKode($fil['kd_prodi']):'';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		$posY += 6;
		$namas = 'Matakuliah    : '.$namas;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($namas) / 2);
		$this->our_pdf->text($posX,$posY,$namas);
		$posY += 6;
		$dosens = 'Dosen           : '.$dosens;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($dosens) / 2);
		$this->our_pdf->text($posX,$posY,$dosens);
		$posY += 6;
		$kelass = 'Kelas	             : '.$kd_kelas;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($kelass) / 2);
		$this->our_pdf->text($posX,$posY,$kelass);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Nilai.pdf", "I");
	}

	//laporan presensi
	function presensi_pdf($kd_prodi,$kd_matakuliah,$kd_dosen,$kd_kelas){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		$fil['kd_kelas'] = $kd_kelas;
		
		 //$data['nama'] = 'indriyanto';
		 $data['namaUniv'] = 'UNIVERSITAS OTTOW GEISSLER';
		 $data['alamatUniv'] = 'Jln. Perkutut Kotaraja, Telp.(0967)581562';
		 $data['kotaUniv'] = 'Jayapura, Papua';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_nilai->get_pdf($fil);
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('perkuliahan/presensi_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('exemple00.pdf');
	}

	function import()
	{
		# --
		$error='';
		$result=true;
		$extensi = '';
		
		# load
		$this->load->helper('file');
		
		# upload filenya
		$fupload = $_FILES['datafile'];
		$nama = $_FILES['datafile']['name'];
		$extensi = $_FILES['datafile']['type'];
		//chan
		$allowedExtensions = array("xls"); 

		if (!in_array(end(explode(".", 
			strtolower($nama))), 
			$allowedExtensions)) { 
			echo json_encode(array('success'=>false, 'msg'=>'File yang akan disuport hanya untuk tipe Excel (xls)'));
			return;
		} 
		if(isset($fupload)){
			$lokasi_file 	= $fupload['tmp_name'];
			$direktori		= "file_upload/perkuliahan/$nama";
			
			if(move_uploaded_file($lokasi_file, $direktori)){ // proses upload					
				//Excel 2003
				# load librari
				$this->load->library('excel');
				$this->excel->setOutputEncoding('CP1251');
				$this->excel->read($direktori); // baca file
				
				$prodi = $this->parse($this->excel->val(4,4));
				$matakuliah = $this->parse($this->excel->val(6,4));
				$dosen = $this->parse($this->excel->val(7,4));

				$tahun = $this->parse($this->excel->val(3, 12));
				$jadwal = $this->parse($this->excel->val(6, 12));

				# baca per baris
				for($i=12, $n=$this->excel->rowcount(0); $i<= $n; $i++){
					# data
					$data['prodi'] 			= $prodi->id;
					$data['matkul'] 		= $matakuliah->id;
					$data['dosen'] 			= $dosen->id;
					$data['tahun'] 			= $tahun->id;
					$data['jadwal'] 		= $jadwal->id;

					$data['nim'] 			= $this->excel->val($i, 2);
					$data['nilai_uts'] 		= $this->excel->val($i, 6);
					$data['nilai_uas'] 		= $this->excel->val($i, 7);
					$data['nilai_tugas']	= $this->excel->val($i, 8);
					$data['nilai_quis']		= $this->excel->val($i, 9);
					$data['presensi'] 		= $this->excel->val($i, 10);
					$data['nilai_final']	= $this->excel->val($i, 10);
					$data['nilai_huruf']	= $this->excel->val($i, 12);
					$data['nilai_huruf']	= $this->excel->val($i, 13);
					
					# proses
					$this->db->flush_cache();
					$this->db->where('b.id_jadwal', $jadwal->id);
					$this->db->where('nim', $data['nim']);
					$this->db->join('tbl_krs_detail b', 'b.id_krs = a.id_krs');
					$this->db->join('tbl_jadwal c', 'c.id_jadwal = b.id_jadwal');
					$ada = $this->db->get('tbl_krs a');
					$x = $ada->row();
					if($ada->num_rows() > 0) {
						$data['id_krs'] = $x->id_krs;
						$data['jadwal'] = $x->id_jadwal;
						$result = $this->_importData($data);
					}
				}
				
			 } else {
				$result=false;
				$error = "Format File tidak diketahui.";
			}

			# clear folder temporari
			delete_files('restore');
		}

		if ($result) {
			echo json_encode(array('success'=>true, 'msg'=>'Data berhasil diimport'));
		} else {
			echo json_encode(array('msg'=>$error));
		}
	}

	function parse($input) {
		$obj = new stdClass;
		$input = explode('(', $input);
		$input[0] = trim($input[0]);
		$input[1] = explode(')', $input[1]);
		$input[1] = trim($input[1][0]);
		$obj->id = $input[1];
		$obj->nama = $input[0];
		return $obj;
	}

	function _importData($data, $update = false)
	{
		//query insert data
		$this->db->flush_cache();
		
		$this->db->set('nilai_uts', $data['nilai_uts']);
		$this->db->set('nilai_uas', $data['nilai_uas']);
		$this->db->set('nilai_tugas', $data['nilai_tugas']);
		$this->db->set('nilai_quis', $data['nilai_quis']);
		$this->db->set('nilai_final', $data['nilai_final']);
		$this->db->set('nilai_huruf', $data['nilai_huruf']);
		$this->db->where('id_krs', $data['id_krs']);
		$this->db->where('id_jadwal', $data['jadwal']);
		$result = $this->db->update('tbl_krs_detail');
		
		$errNo   = $this->db->_error_number();
	    $errMess = $this->db->_error_message();
		$error = $errMess;
		//var_dump($errMess);die;
	    log_message("error", "Problem import Inserting to : ".$errMess." (".$errNo.")"); 
		//return
		if($result) {
			return TRUE;
		}
		return FALSE;
	}
}